package com.cqc.jdk8.Jutil.concurrent.atomic;

import org.junit.Test;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @author cui
 * @date 2023/11/10 20:53
 * @description java.util.concurrent.atomic 下的类 都是并发原子类，有很多个类。
 **/
public class AtomicLongTest {


    @Test
    public void f9() {

        AtomicLong atomic = new AtomicLong(100);
        long result = atomic.incrementAndGet();
        System.out.println(result);//101
        System.out.println(atomic.get());//101
    }

    @Test
    public void f8() {
        AtomicLong atomic = new AtomicLong(100);
        long result = atomic.getAndIncrement();
        System.out.println(result);//100
        System.out.println(atomic.get());//101
    }



    @Test
    public void f7() {
        AtomicLong atomic = new AtomicLong(100);
        long result = atomic.getAndDecrement();
        System.out.println(result);//100
        System.out.println(atomic.get());//99
    }

    @Test
    public void f6() {
        AtomicLong atomic = new AtomicLong(100);
        long result = atomic.decrementAndGet();
        System.out.println(result);//99
        System.out.println(atomic.get());//99
    }

    @Test
    public void f5() {
        //如果initialValue=expect，那么会重新赋值为update；如果不等，那么保持原值
        AtomicLong atomic = new AtomicLong(100);
        boolean b = atomic.compareAndSet(100, 6);
        System.out.println(b);//true
        System.out.println(atomic.get());//6

        AtomicLong atomic2 = new AtomicLong(100);
        boolean b2 = atomic.compareAndSet(99, 6);
        System.out.println(b2);//false
        System.out.println(atomic2.get());//100
    }

    @Test
    public void f4() {
        AtomicLong atomic = new AtomicLong(100);
        long result = atomic.getAndAdd(3);
        System.out.println(result);//100
        System.out.println(atomic.get());//103
    }

    @Test
    public void f3() {
        AtomicLong atomic = new AtomicLong(100);
        long addAndGet = atomic.addAndGet(3);
        System.out.println(addAndGet);//103
        System.out.println(atomic.get());//103
    }

    @Test
    public void f2() {
        AtomicLong atomic = new AtomicLong(100);
        System.out.println(atomic.get());
    }

    @Test
    public void f1() {
        AtomicLong atomic = new AtomicLong();
        long l = atomic.get();
        System.out.println(atomic.get());
    }
}
